package ch08_Swing高级组件.JSpinner;

import ch07_GUI编程.BaseFrame;

import javax.swing.*;
import java.awt.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 挚爱之夕
 * @date 2021/8/26 - 08 - 26 - 22:31
 * @Description: ch08.JSpinner
 * @Version: 1.0
 */
public class JSpinnerDemo {
    public static void main(String[] args) throws ParseException {
        BaseFrame f = new BaseFrame("JSpinner 演示");
        f.setLayout(new FlowLayout());

        JSpinner s0 = new JSpinner(); //默认微调按钮
        JSpinner s1 = new JSpinner(new SpinnerNumberModel(120,0,255,1));//数值型

        String[] weeks = {"周一","周二","周三","周四","周五","周六","周日"};
        JSpinner s2 = new JSpinner(new SpinnerListModel(weeks));    //列表型
        ((JSpinner.DefaultEditor)s2.getEditor()).getTextField().setEditable(false);//禁止编辑

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //日期格式化器
        Date now = new Date();
        Date min = format.parse("1995-01-01");  //最小日期
        Date max = format.parse("2022-12-31");  //最大日期

        SpinnerDateModel m3 = new SpinnerDateModel(now,min,max, Calendar.DAY_OF_WEEK);
        JSpinner s3 = new JSpinner(m3); //日期型
        JSpinner.DateEditor editor = new JSpinner.DateEditor(s3,"yyyy年MM月dd日");//编辑器
        s3.setEditor(editor);   //设置编辑器

        f.add(s0);
        f.add(s1);
        f.add(s2);
        f.add(s3);
        f.showMe();
    }
}
